﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_Policy")]
    public class P_Policy : IEntity<P_Policy>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string PolicyName { get; set; }
        public int DurationType { get; set; }
        public int Duration { get; set; }
        public int BalanceType { get; set; }
        /// <summary>
        /// 计算方式
        /// </summary>
        public int SettlementType { get; set; }
        public decimal Rate { get; set; }
        [Column(TypeName = "bit")]
        public bool IsAttach { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public DateTime CreateTime { get; set; }
        public int CycleAmount { get; set; }

        public override P_Policy Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_Policy Find(System.Linq.Expressions.Expression<Func<P_Policy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Policy.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.IsAttach = item.IsAttach;
                    this.PolicyName = item.PolicyName;
                    this.BalanceType = item.BalanceType;
                    this.SettlementType = item.SettlementType;
                    this.Rate = item.Rate;
                    this.Status = item.Status;
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.DurationType = item.DurationType;
                    this.Duration = item.Duration;
                    this.CycleAmount = item.CycleAmount;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_Policy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Policy.Any(where);
            }
        }
    }
}